<?php

/**
 * @Project NUKEVIET 3.0
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2010 VINADES.,JSC. All rights reserved
 * @Createdate 2-1-2010 21:40
 */

if ( ! defined( 'NV_IS_FILE_SETTINGS' ) ) die( 'Stop!!!' );

$select_options[NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&amp;" . NV_OP_VARIABLE . "=cronjobs_add"] = $lang_module['nv_admin_add'];

$sql = "SELECT * FROM `" . NV_CRONJOBS_GLOBALTABLE . "` ORDER BY `is_sys` DESC";
$result = $db->sql_query( $sql );

if ( ! $db->sql_numrows( $result ) )
{
    Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&amp;" . NV_OP_VARIABLE . "=cronjobs_add" );
    die();
}

$contents = array();

while ( $row = $db->sql_fetchrow( $result ) )
{
    $contents[$row['id']]['caption'] = isset( $row[NV_LANG_INTERFACE . '_cron_name'] ) ? $row[NV_LANG_INTERFACE . '_cron_name'] : ( isset( $row[NV_LANG_DATA . '_cron_name'] ) ? $row[NV_LANG_DATA . '_cron_name'] : $row['run_func'] );
    $contents[$row['id']]['edit'] = array(
        ( empty( $row['is_sys'] ) ? 1 : 0 ), $lang_global['edit'], NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&amp;" . NV_OP_VARIABLE . "=cronjobs_edit&amp;id=" . $row['id'] 
    );
    $contents[$row['id']]['delete'] = array(
        ( empty( $row['is_sys'] ) ? 1 : 0 ), $lang_global['delete'] 
    );
    $contents[$row['id']]['disable'] = array(
        ( ( empty( $row['is_sys'] ) or empty( $row['act'] ) ) ? 1 : 0 ), ( $row['act'] ? $lang_global['disable'] : $lang_global['activate'] ), NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&amp;" . NV_OP_VARIABLE . "=cronjobs_act&amp;id=" . $row['id'] 
    );
    $contents[$row['id']]['detail'][$lang_module['run_file']] = $row['run_file'];
    $contents[$row['id']]['detail'][$lang_module['run_func']] = $row['run_func'];
    $contents[$row['id']]['detail'][$lang_module['params']] = ! empty( $row['params'] ) ? implode( ", ", explode( ",", $row['params'] ) ) : "";
    $contents[$row['id']]['detail'][$lang_module['start_time']] = nv_date( "l, d/m/Y H:i", $row['start_time'] );
    $contents[$row['id']]['detail'][$lang_module['interval']] = nv_convertfromSec( $row['interval'] * 60 );
    $contents[$row['id']]['detail'][$lang_module['is_del']] = ! empty( $row['del'] ) ? $lang_module['isdel'] : $lang_module['notdel'];
    $contents[$row['id']]['detail'][$lang_module['is_sys']] = ! empty( $row['is_sys'] ) ? $lang_module['system'] : $lang_module['client'];
    $contents[$row['id']]['detail'][$lang_module['act']] = ! empty( $row['act'] ) ? $lang_module['act1'] : $lang_module['act0'];
    $contents[$row['id']]['detail'][$lang_module['last_time']] = ! empty( $row['last_time'] ) ? nv_date( "l, d/m/Y H:i", $row['last_time'] ) : $lang_module['last_time0'];
    $contents[$row['id']]['detail'][$lang_module['last_result']] = empty( $row['last_time'] ) ? $lang_module['last_result_empty'] : $lang_module['last_result' . $row['last_result']];
    
    $interval = $row['interval'] * 60;
    
    if ( empty( $row['act'] ) )
    {
        $next_time = "n/a";
    }
    else
    {
        if ( empty( $interval ) )
        {
            $next_time = nv_date( "l, d/m/Y H:i", max( $row['start_time'], NV_CURRENTTIME ) );
        }
        else
        {
            $next_time = nv_date( "l, d/m/Y H:i", $row['start_time'] + ceil( ( NV_CURRENTTIME - $row['start_time'] ) / $interval ) * $interval );
        }
    }
    
    $contents[$row['id']]['detail'][$lang_module['next_time']] = $next_time;
}

$contents = call_user_func( "main_theme", $contents );
$page_title = $lang_global['mod_cronjobs'];

include ( NV_ROOTDIR . "/includes/header.php" );
echo nv_admin_theme( $contents );
include ( NV_ROOTDIR . "/includes/footer.php" );

?>